package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;

import com.example.myapplication.bean.ObservableUser;
import com.example.myapplication.coroutines.CoroutinesActivityJ;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.databinding.BindAdapterActivity;
import com.example.myapplication.databinding.DataBindingActivity;
import com.example.myapplication.livedata.LiveDataEnterActivity;
import com.example.myapplication.navigation.FragmentEnterActivity;
import com.example.myapplication.navigation.RecoverFragmentActivity;
import com.example.myapplication.observable.ObservableActivity;
import com.example.myapplication.scroller.ScrollerActivity;
import com.example.myapplication.utils.LoadingDialogUtil;

import androidx.appcompat.app.AppCompatActivity;

import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;


import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private int version=2;
    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;
    private int ageNum = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnGoFragment.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, FragmentEnterActivity.class);
            startActivity(intent);
        });

        binding.btnGoRecoverFragment.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, RecoverFragmentActivity.class);
            startActivity(intent);
        });

        binding.btnGoCustomScroller.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, ScrollerActivity.class);
            startActivity(intent);
        });

        binding.btnGoDatabinding.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, DataBindingActivity.class);
            startActivity(intent);
        });

        binding.btnGoBindingAdapter.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, BindAdapterActivity.class);
            startActivity(intent);
        });

        binding.btnGoLiveData.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, LiveDataEnterActivity.class);
            startActivity(intent);

        });

        binding.btnGoObservable.setOnClickListener(v -> {
            Intent intent=new Intent();
            intent.setClass(MainActivity.this, ObservableActivity.class);
            startActivity(intent);
        });

        binding.btnGoCoroutines.setOnClickListener(v -> {
            Intent intent=new Intent();
            intent.setClass(MainActivity.this, CoroutinesActivityJ.class);
            startActivity(intent);
        });

    }

    private void showDialog() {
        LoadingDialogUtil.show(this);
        //new Thread(() -> LoadingDialogUtil.dismiss()).start();
        //new Handler().postDelayed(() -> LoadingDialogUtil.dismiss(),3000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public static class ClickHandler {
        private int num = 0;
        private ObservableUser observableUser;

        public ClickHandler(ObservableUser user) {
            this.observableUser = user;
        }

        public void changeName() {
            num++;
            observableUser.setName("姓名：龙哥" + num);
        }

        public void changeAge() {
            num++;
            observableUser.setAge(num);
        }

        public void changeVisible() {
            observableUser.setVisible(!observableUser.isVisible());
        }

        public void changeAll() {
            changeName();
            changeAge();
            changeVisible();
        }

        public void changeAllAndNotify() {
            changeAll();
            onlyNotifyAll();
        }

        public void onlyNotifyAll() {
            observableUser.notifyChange();
        }

        public void onlyNotifyName() {
            observableUser.notifyPropertyChanged(BR.name);
        }
    }


    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}
